package link

import (
	"TD-UI/api"
	v1 "TD-UI/api/v1"
	"TD-UI/internal/model"
	"TD-UI/internal/service"
	"context"
	"github.com/gogf/gf/v2/util/gconv"
	_ "github.com/taosdata/driver-go/v3/taosRestful"
)

type ControllerLink struct{}

func NewV1() api.ILinkV1 {
	return &ControllerLink{}
}

func (c *ControllerLink) Link(ctx context.Context, req *v1.LinkReq) (res *v1.LinkRes, err error) {
	tData := model.LinkReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Link().Link(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *ControllerLink) Add(ctx context.Context, req *v1.LinkAddReq) (res *v1.LinkRes, err error) {
	tData := model.LinkAddReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Link().Add(ctx, tData)
	gconv.Struct(result, &res)
	return

}

func (c *ControllerLink) GetAll(ctx context.Context, req *v1.LinkGetReq) (res *v1.LinkRes, err error) {
	tData := model.LinkGetReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Link().GetAll(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *ControllerLink) Del(ctx context.Context, req *v1.LinkDelReq) (res *v1.LinkRes, err error) {
	tData := model.LinkDelReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	_ = service.Link().Del(ctx, tData)
	return
}
